using GraphEditor.Runtime;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace GraphEditor.Editor
{
    public class GraphEditorWindow : EditorWindow
    {

        /// <summary>
        /// 当前窗口持有的画布
        /// </summary>
        private GraphCanvas _activeGraphCanvas;
        /// <summary>
        /// 当前画布对应的编辑器
        /// </summary>
        private BaseGraphEditor _activeEditor;

        private void OnEnable()
        {
            // 订阅选中物体变化的事件
            Selection.selectionChanged += OnSelectionChanged;
        }

        private void OnDisable()
        {
            Selection.selectionChanged -= OnSelectionChanged;
        }

        private void OnSelectionChanged()
        {
            Object selectedObject = Selection.activeObject;
            if (selectedObject is GraphCanvas graphCanvas)
            {
                Debug.LogError("选中GraphCanvas");
                _activeGraphCanvas = graphCanvas;
            }
            else if (selectedObject is GameObject gameObject)
            {
                GraphCanvasHolder graphCanvasHolder = gameObject.GetComponent<GraphCanvasHolder>();
                if (graphCanvasHolder != null)
                {
                    Debug.LogError("选中GraphCanvasHolder");
                    _activeGraphCanvas = graphCanvasHolder.graphCanvas;
                }
            }
            // 关键步骤：根据 runtimeType 创建对应的编辑器实例
            if (_activeGraphCanvas != null && !string.IsNullOrEmpty(_activeGraphCanvas.LogicType))
            {
                _activeEditor = GraphEditorRegistry.CreateEditor(_activeGraphCanvas.LogicType);
                if (_activeEditor != null)
                {
                    // 开始初始化
                }
            }
            else
            {
                // 如果没有指定类型，则清空
                _activeEditor = null;
            }
            // 刷新窗口
            Repaint();
        }

        /// <summary>
        /// 通过graphCanvasAsset打开编辑器
        /// </summary>
        /// <param name="graphCanvas"></param>
        public static void OpenWindow(GraphCanvas graphCanvas)
        {
            GraphEditorWindow window = GetWindow<GraphEditorWindow>("节点编辑器");
            window._activeGraphCanvas = graphCanvas;
        }

        /// <summary>
        /// 通过instance打开编辑器
        /// </summary>
        /// <param name="instance"></param>
        public static void OpenWindow(object instance)
        {
            GraphEditorWindow window = GetWindow<GraphEditorWindow>("节点编辑器");
        }


        [MenuItem("可视化节点编辑器/Show My DropDown")]
        public static void ShowWindow()
        {
            GetWindow<GraphEditorWindow>("My Editor");
        }

        private void CreateGUI()
        {
            Button button = new Button() { text = "Show DropDown" };
            button.clicked += () =>
            {
                Vector2 screenPoint = this.rootVisualElement.LocalToWorld(Event.current.mousePosition);

                // 为 "buttonRect" 创建一个大小为0的Rect，位置是鼠标的屏幕坐标
                Rect mouseScreenRect = new Rect(screenPoint + position.position, Vector2.zero);

                // 将这个屏幕坐标系的 Rect 传递过去
                CanvasPopupWindow.Show(mouseScreenRect, new Vector2(200, 150));
            };
            rootVisualElement.Add(button);
        }
    }
}